package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/** 集合与数组互转 */
public class CollectionToArray {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        System.out.println(c);//[one, two, three, four, five]

        /* 1.若参数数组元素个数== 集合元素个数。那就正常转换
        *  2.若参数数组元素个数 > 集合元素个数。那也正常转换(按照集合大小给数值)
        *  3.若参数数组元素个数 < 集合元素个数。那也正常转换，同时在末尾补默认值
        */
        String [] array = c.toArray(new String[5]);
        System.out.println(Arrays.toString(array));//[one, two, three, four, five]
        String [] array1 = c.toArray(new String[3]);
        System.out.println(Arrays.toString(array1));//[one, two, three, four, five]
        String [] array2 = c.toArray(new String[7]);
        System.out.println(Arrays.toString(array2));//[one, two, three, four, five, null, null]
    }
}
